//
//  switchBar.m
//  Etion
//
//  Created by hua ouyang on 11-10-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "switchBar.h"

@implementation CSwitchBarTitleCellView

@synthesize m_title;
@synthesize m_direction;
@synthesize m_font;
@synthesize m_selectedFont;
@synthesize m_titleColor;
@synthesize m_backgroundColor;
@synthesize m_titleSelectedColor;
@synthesize m_backgroundSelectedColor;
@synthesize m_isSelected;
@synthesize m_index;
@synthesize m_delegate;
@synthesize m_bottomLineImage;
@synthesize m_backImage;
@synthesize m_backSelectedImage;

- (id)initWithTitle:(NSString *)title rect:(CGRect)rect
{
    if(self = [super initWithFrame:rect])
    {
        self.m_title = title;
        self.m_font = [UIFont systemFontOfSize:14];
        self.m_selectedFont = [UIFont systemFontOfSize:16];
        m_direction = ESWitchBarDirectionTop;
        self.m_titleColor = [UIColor blackColor];
        self.m_titleSelectedColor = [UIColor blackColor];
        self.m_backgroundColor = [UIColor colorWithRed:140 green:180 blue:225 alpha:1];
        self.m_backgroundSelectedColor = [UIColor orangeColor];
        m_isSelected = NO;
        [self initDrawPointArrayAndTextRect];
    }
    return self;
}

- (void)initDrawPointArrayAndTextRect
{
    float width = 10;
    CGRect rect = self.frame;
    switch (m_direction) {
        case ESWitchBarDirectionTop:
        {
            m_P1 = CGPointMake(0, rect.size.height);
            m_P2 = CGPointMake(width, 0);
            m_P3 = CGPointMake(rect.size.width-width, 0);
            m_P4 = CGPointMake(rect.size.width, rect.size.height);
            m_textRect = CGRectMake(width, 0, rect.size.width-2*width, rect.size.height);
            break;
        }
        case ESWitchBarDirectionRight:
        {
            m_P1 = CGPointMake(0, 0);
            m_P2 = CGPointMake(rect.size.width, width);
            m_P3 = CGPointMake(rect.size.width, rect.size.height-width);
            m_P4 = CGPointMake(0, rect.size.height);
            m_textRect = CGRectMake(3, width, rect.size.width-2*3, rect.size.height-2*width);
            break;
        }
        case ESWitchBarDirectionBottom:
        {
            m_P1 = CGPointMake(0, 0);
            m_P2 = CGPointMake(rect.size.width, 0);
            m_P3 = CGPointMake(rect.size.width-width, rect.size.height);
            m_P4 = CGPointMake(width, rect.size.height);
            m_textRect = CGRectMake(width, 0, rect.size.width-2*width, rect.size.height);
            break;
        }
        case ESWitchBarDirectionLeft:
        {
            m_P1 = CGPointMake(0, width);
            m_P2 = CGPointMake(rect.size.width, 0);
            m_P3 = CGPointMake(rect.size.width, rect.size.height);
            m_P4 = CGPointMake(0, rect.size.height-width);
            m_textRect = CGRectMake(3, width, rect.size.width-2*3, rect.size.height-2*width);
            break;
        }
        default:
            break;
    }
}

- (void)drawRectUsedColor:(CGRect)rect
{
    if(m_isSelected)
        [m_backgroundSelectedColor setFill];
    else
        [m_backgroundColor setFill];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:m_P1];
    [path addLineToPoint:m_P2];
    [path addLineToPoint:m_P3];
    [path addLineToPoint:m_P4];
    [path closePath];
    [path fill];
}

- (void)backgroundImage:(UIImage*)backImage selected:(UIImage*)selectedImage bottomLine:(UIImage*)lineImage
{
    self.m_backImage = backImage;
    self.m_backSelectedImage = selectedImage;
    self.m_bottomLineImage = lineImage;
    
    if(backImage!=nil && selectedImage!=nil && lineImage!=nil)
        m_isUsedImage = YES;
}

- (void)drawRectUsedImage:(CGRect)rect
{
    float space = 2.0;
    float leftWidth = 10;
    float dis = 4;
    if(m_isSelected)
    {
        [m_bottomLineImage drawInRect:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 2) blendMode:kCGBlendModeNormal alpha:1.0];
        UIImage *image = [m_backSelectedImage stretchableImageWithLeftCapWidth:leftWidth topCapHeight:0];
        [image drawInRect:CGRectMake(space, 0, self.frame.size.width-2*space, self.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    }
    else
    {
        UIImage *image = [m_backImage stretchableImageWithLeftCapWidth:leftWidth topCapHeight:0];
        [image drawInRect:CGRectMake(space, dis, self.frame.size.width-2*space, self.frame.size.height-dis) blendMode:kCGBlendModeNormal alpha:1.0];
        [m_bottomLineImage drawInRect:CGRectMake(0, rect.size.height-1, rect.size.width, 2) blendMode:kCGBlendModeNormal alpha:1.0];
    }
}

- (void)drawRect:(CGRect)rect
{
    if(m_isUsedImage)
        [self drawRectUsedImage:rect];
    else
        [self drawRectUsedColor:rect];
    
    if(m_title != nil)
    {
        UIFont *font = m_font;
        CGRect rc = CGRectZero;
        if(m_isSelected)
        {
            [m_titleSelectedColor set];
            font = m_selectedFont;
            CGSize expectedSize = [m_title sizeWithFont:font constrainedToSize:CGSizeMake(296, 9999) lineBreakMode:UILineBreakModeHeadTruncation];
            rc = CGRectMake(m_textRect.origin.x, (m_textRect.size.height-expectedSize.height)/2.0, m_textRect.size.width, expectedSize.height);
        }
        else
        {
            [m_titleColor set];
            CGSize expectedSize = [m_title sizeWithFont:font constrainedToSize:CGSizeMake(296, 9999) lineBreakMode:UILineBreakModeHeadTruncation];
            rc = CGRectMake(m_textRect.origin.x, (m_textRect.size.height-expectedSize.height-4.0)/2.0+4.0, m_textRect.size.width, expectedSize.height);
        }
        
        [m_title drawInRect:rc withFont:font lineBreakMode:UILineBreakModeHeadTruncation alignment:UITextAlignmentCenter];
    }
}

- (void)dealloc
{
    self.m_font = nil;
    self.m_selectedFont = nil;
    self.m_title = nil;
    [m_titleColor release];
    [m_titleSelectedColor release];
    [m_backgroundColor release];
    [m_backgroundSelectedColor release];
    [m_bottomLineImage release];
    [m_backImage release];
    [m_backSelectedImage release];
    [super dealloc];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    m_isSelected = YES;
	if([m_delegate respondsToSelector:@selector(didSelected:)])
		[m_delegate didSelected:self];
}

@end


@implementation CSwitchBar

@synthesize m_titleArray;
@synthesize m_delegate;

- (id)initWithFrame:(CGRect)frame titleArray:(NSMutableArray *)array index:(int)index
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:155.0/255.0 green:209.0/255.0 blue:1.0 alpha:1.0];
        UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"switchBar_bottomLine.png"]];
        imageview.frame = CGRectMake(0, frame.size.height-1, frame.size.width, 1);
        [self addSubview:imageview];
        [imageview release];
        
        self.m_titleArray = array;
        m_backview = [[UIScrollView alloc]initWithFrame:CGRectMake(m_leftView.frame.size.width, 5, frame.size.width, frame.size.height-5)];
        [self addSubview:m_backview];
        
        m_currentIndex = index;
        float width = 70.0;
        float height = m_backview.frame.size.height;
        float startX = 0.0;
        int i = 0;
        for(NSString *str in array)
        {
            CSwitchBarTitleCellView *view = [[CSwitchBarTitleCellView alloc]initWithTitle:str rect:CGRectMake(startX, 0, width, height)];
            [view backgroundImage:[UIImage imageNamed:@"switchBar.png"] selected:[UIImage imageNamed:@"switchBar_selected.png"] bottomLine:[UIImage imageNamed:@"switchBar_bottomLine.png"]];
            if(i==m_currentIndex)
            {
                view.m_isSelected = YES;
                m_currentSwitchView = view;
            }
            view.m_index = i;
            view.m_delegate = self;
            view.backgroundColor = [UIColor clearColor];
            [m_backview addSubview:view];
            [view release];
            startX += width;
            i++;
        }
        m_backview.showsHorizontalScrollIndicator = NO;
        m_backview.showsVerticalScrollIndicator = NO;
        m_backview.contentSize = CGSizeMake(MAX(startX, m_backview.frame.size.width), m_backview.frame.size.height);
    }
    return self;
}

- (void)didSelected:(CSwitchBarTitleCellView*)titleCellView
{
    if(m_currentIndex != titleCellView.m_index)
    {
        m_currentSwitchView.m_isSelected = NO;
        [m_currentSwitchView setNeedsDisplay];
        //
        //添加动画
        //
        [titleCellView setNeedsDisplay];
        m_currentIndex = titleCellView.m_index;
        m_currentSwitchView = titleCellView;
        
        if([m_delegate respondsToSelector:@selector(switchBar:didSelectTitleAtIndex:)])
            [m_delegate switchBar:self didSelectTitleAtIndex:m_currentIndex];
    }
}

-(void)dealloc
{
    [m_backview release];
    [m_leftView release];
    [m_rightView release];
    [m_titleArray release];
    [super dealloc];
}

@end
